part of '../page.dart';

class _CollectionTile extends StatefulWidget {
  const _CollectionTile({
    required this.title,
    required this.onPressed,
    required this.trailing,
    this.showPremiumBadge = false,
  });

  final String title;
  final VoidCallback? onPressed;
  final Widget trailing;
  final bool showPremiumBadge;

  @override
  _CollectionTileState createState() => _CollectionTileState();
}

class _CollectionTileState extends State<_CollectionTile> {
  bool _pressed = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: _pressed ? CoreTheme.of(context).pressedTileColor : CoreTheme.of(context).tileColor,
      child: Row(
        children: [
          Expanded(
            child: GestureDetector(
              behavior: HitTestBehavior.translucent,
              onTapDown: (tapDownDetails) {
                if (widget.onPressed != null) {
                  setState(() {
                    _pressed = true;
                  });
                }
              },
              onTapCancel: () {
                if (widget.onPressed != null) {
                  setState(() {
                    _pressed = false;
                  });
                }
              },
              onTap: () {
                if (widget.onPressed != null) {
                  widget.onPressed!();
                  Future.delayed(kTilePressEffectDuration, () {
                    _pressed = false;
                    if (mounted) {
                      setState(() {});
                    }
                  });
                }
              },
              child: DefaultSizeTile(
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: Padding(
                    padding: const EdgeInsets.only(
                      left: 16,
                      top: 8,
                      bottom: 8,
                    ),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Flexible(
                          child: Text(
                            widget.title,
                            style: CoreTheme.of(context).textTheme.tileTitle,
                            textWidthBasis: TextWidthBasis.longestLine,
                          ),
                        ),
                        if (widget.showPremiumBadge) ...[
                          const TextTilePremiumBadgeGap(),
                          const TilePremiumBadge(),
                        ],
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
          widget.trailing,
        ],
      ),
    );
  }
}
